package com.ftdigital.primemovers_operators.adpter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.model.OilReprotEntity;
import com.ftdigital.primemovers_operators.model.OilReprotItme;


/**
 * Created by Curry on 2015-12-16.
 */
public class OilReportReleaseListViewAdapter extends RecyclerView.Adapter<OilReportReleaseListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private OilReprotEntity oilReprotItme;
    public OilReportReleaseListViewAdapter(Context context, OilReprotEntity oilReprotItme) {
        mContext = context;
        this.oilReprotItme = oilReprotItme;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public OilReportReleaseListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.online_station_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OilReportReleaseListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        SwitchBg(holder, position);
    }

    @Override
    public int getItemCount() {
        return 3;
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void SwitchBg(OilReportReleaseListViewAdapter.ViewHolder holder, int position){
        switch (position % 3){
            case 0:
                holder.getLayout().setBackground(mContext.getResources().getDrawable(R.drawable.circle_blue_bg));
               holder.getNum().setText(oilReprotItme.getData().getDays());
                holder.getStationName().setText(mContext.getResources().getString(R.string.addoil_days));
                break;
            case 1:
                holder.getLayout().setBackground(mContext.getResources().getDrawable(R.drawable.circle_orage_bg));
                holder.getNum().setText(oilReprotItme.getData().getWeeks());
                holder.getStationName().setText(mContext.getResources().getString(R.string.addoil_week));
                break;
            case 2:
                holder.getLayout().setBackground(mContext.getResources().getDrawable(R.drawable.circle_red_bg));
                holder.getNum().setText(oilReprotItme.getData().getLast_months());
                holder.getStationName().setText(mContext.getResources().getString(R.string.addoil_month));
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private RelativeLayout layout;
        private TextView stationName;
        private TextView num;
        public ViewHolder(View itemView) {
            super(itemView);
            layout = (RelativeLayout) itemView.findViewById(R.id.layout);
            stationName = (TextView) itemView.findViewById(R.id.station_name);
            num = (TextView) itemView.findViewById(R.id.num);
        }

        public RelativeLayout getLayout() {
            return layout;
        }

        public void setLayout(RelativeLayout layout) {
            this.layout = layout;
        }

        public TextView getStationName() {
            return stationName;
        }

        public void setStationName(TextView stationName) {
            this.stationName = stationName;
        }

        public TextView getNum() {
            return num;
        }

        public void setNum(TextView num) {
            this.num = num;
        }
    }
}
